<?php
require_once '../models/Common.php';
require_once '../models/Customer.php';

if(isset($_POST["btn_submit"]))
{
	$f_name=$_POST["f_name"];
	$l_name=$_POST["l_name"];
	$gender=$_POST["gender"];
	$day=$_POST["ngay"];
	$month=$_POST["thang"];
	$year=$_POST["nam"];
	$email=$_POST["email"];
	$password=$_POST["password"];
	$phone=$_POST["phone"];
	$address=$_POST["address"];
	$city=$_POST["city"];

	$_msg=null;
	$error=null;	
	$check=true;
	$_msg_n=null;
	$_msg_em=null;
	$_msg_dt=null;
	$_msg_mk=null;

	{//check dữ liệu trống
	if(strlen($password)>5){
		$password=stripcslashes($password);
	}else{
	$check=false;
	$_msg_mk='<b><span class="error"><br>Mật khẩu tối đa phải 6 ký tự</span></b>';
	}
	//check dinh dang dien thoai
		if(preg_match("/^[0-9]{11}$/",$phone)){
		$_msg2=null;
		}else{
		$_msg_dt='<b><span class="error"><br>Vui lòng nhập đúng số điện thoại</span></b>';
		$check=false;
		}
		//check dinh dang email
		/*if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
			$_msg_em='<b><span class="error"><br>Email chưa đúng.</span></b>';
			$check=false;
		}*/
		//check f_name & l_name
		if(preg_match("/[a-zA-Z]/",$f_name) &&preg_match("/[a-zA-Z]/",$l_name)){
		}else{
		$_msg_n='<b><span class="error">Vui lòng nhập đúng họ và tên.</span></b>';
		$check=false;
		}

	if($check==true){
	//check email trong sql da ton tai hay chua
		$checkEmail=Customer::checkEmail($email);
		if($checkEmail==true){
			//Dang ky user moi
			$re=Customer::addCustomer($f_name,$l_name,$gender,$day,$month,$year,$email,$password,$phone,$address,$city);
			if($re){
				echo "<script>alert('Đăng ký thành công!');window.location='login.php'</script>";
			}else $_msg.="Lỗi";
		}else{
			$checkEmail=false;
			$_msg_em.='<b><span class="error"><br>Email đã có người sử dụng!</span></b>';
		}
	}
	else
	{	$error='<b><span class="error"><br>Đăng ký thất bại!</span></b>';}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Đăng Ký</title>
<?php include '_meta.php';?>
<link rel="stylesheet" type="text/css" href="../css/register.css" />

</head>
<body>
	<div id="firstLine">
		<div id="top-bar">
		<?php include '_top_bar.php';?>
		</div>
	</div>
	<div id="container">
		<div id="header">
			<?php include '_head.php';?>
		</div>
		<?php include '_menu_head.php';?>
		<div id="center">
			<div id="menu-left">
				<?php include '_menu_left.php';?>
			</div>
			<div id="table">
				<form name="myForm" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
				<div id="div_login">
				<div id="content_login">
					<div id="head_login">
						<div class="lbl_"><h2></h2></div>
						
					</div>
					<div id="article_login">
						<div id="article">
						<div id="head">
		<div class="lbl_"><h2>Đăng ký</h2></div>
		<div id="border-bottom_w320_h2"<div>
	</div>
		<div>
			<input type="text" class="input_text_w150_h30" id="f_name" name="f_name" value="<?php if(isset($_POST["btn_submit"])) echo $f_name;?>" placeholder="Tên"/>
			<input type="text" class="input_text_w150_h30" id="l_name" name="l_name" value="<?php if(isset($_POST["btn_submit"])) echo $l_name;?>" placeholder="Họ"/>
			<span></span>
		</div>
		<span class="error_n">
		<?php if(isset($_POST["btn_submit"])) echo $_msg_n; ?>
		</span>
		<div id="login">
			<input type="email" class="input_text_w312_h30" id="email" name="email" value="<?php if(isset($_POST["btn_submit"])) echo $email;?>" placeholder="Nhập tên Email"/>
		</div>
		<div>
			<input type="password" class="input_text_w312_h30" id="password" name="password" value="<?php if(isset($_POST["btn_submit"])) echo $password;?>" placeholder="Nhập mật khẩu"/>
			<span><?php if(isset($_POST["btn_submit"])) echo $_msg_mk; ?></span>
		</div>
		<div id="gender">
			<div class="floatleft"><span>Giới tính:</span></div>
			<div class="floatright"><select class="select_h30px" name="gender">
			<option value="Nam">Nam</option>
			<option value="Nữ">Nữ</option>
			</select></div>
		</div>
		<div id="birthday">	
			<div class="floatleft"><span>Ngày sinh:</span></div>
			<div class="floatright"><?php
				Common::dropBday();
				Common::dropBmonth();
				Common::dropByear();
			?></div>
		</div>
		<div>
			<div class="floatleft"><span>Điện thoại:</span></div>
			<div class="floatright"><input type="text" class="input_text_w235_h30" id="phone" name="phone" value="<?php if(isset($_POST["btn_submit"])) echo $phone;?>"placeholder="Nhập số điện thoại di động"></div>
			<span class="error_n"><?php if(isset($_POST["btn_submit"])) echo $_msg_dt; ?></span>
		</div>
		<div id="">
			<div class="floatleft"><span>Địa chỉ:</span></div>
			<div class="floatright"><input type="text" class="input_text_w235_h30" id="address" name="address" value="<?php if(isset($_POST["btn_submit"])) echo $address;?>" placeholder="Nhập địa chỉ"></div>
		</div>
		<div id="">
			<div class="floatleft"><span>Thành phố:</span></div>
			<div class="floatright"><input type="text" class="input_text_w235_h30" class="city" name="city" value="<?php if(isset($_POST["btn_submit"])) echo $city;?>" placeholder="Nhập Thành phố"></div>
		</div>
		<div id="button_dk">
			<input type="submit" class="btn_button_" name="btn_submit" value="Đăng ký"/>
			<a href="index.php"><input type="button" class="btn_button_" name="btnCancel" value="Quay lại"></a>
			<?php 
				if(isset($_POST["btn_submit"])){
					if($check==false ||$checkEmail==false)
					{	echo $error;
						echo $_msg;
					}
				}?>
			</div>
		</div>
					</div>
				</div>
				
				</div>
				<div>
				
				</form>
				
			</div>
			<div id="menu-left">
				<?php //include '_menu_left.php';?>
			</div>
			</div>
		</div>
	</div>
	</div>
	<div id="footer">
		<?php include '_footer.php';?>
	</div>
</body>
</html>